<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Collection
 *
 * @author ketu
 */
class Zhishidi_Question_Model_Mysql4_Question_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract {

    protected function _construct() {
        $this->_init('zhishidi_question/question');
    }

    public function joinProducts() {
        $tableName = Mage::getSingleton('core/resource')->getTableName('catalog/product');
        $productResource = Mage::getResourceSingleton('catalog/product');
        $nameAttr = $productResource->getAttribute('name');
        $nameAttrId = $nameAttr->getAttributeId();
        $nameAttrTable = $nameAttr->getBackend()->getTable();

        $this->getSelect()
             ->joinLeft(
                        array('_table_product_name' => $nameAttrTable),
                        '(_table_product_name.entity_id = main_table.product_id)
                             AND (_table_product_name.attribute_id = ' . (int) $nameAttrId . ')',
                        array())
             ->joinLeft(
                        array('_table_product_sku' => $tableName),
                        '(_table_product_name.entity_id = _table_product_sku.entity_id)',
                        array('product' => 'CONCAT(_table_product_name.value, " --- ", _table_product_sku.sku)'));
        return $this;
    }

    public function addVisibleFilter($visible = 'public') {
        $this->getSelect()->where('main_table.status = ?', $visible);
        return $this;
    }

    public function addStoreFilter($store, $withAdmin = true) {
        if ($store instanceof Mage_Core_Model_Store) {
            $store = array($store->getId());
        }
        $this->getSelect()->where('main_table.store_id in (?)', ($withAdmin ? array(0, $store) : $store));
        return $this;
    }


    public function joinStore() {
        $resource = Mage::getSingleton('core/resource');
        $store_table = $resource->getTableName('core_store');
        $this->getSelect()->joinInner(array('_table_store' => $store_table),
                '_table_store.store_id=main_table.store_id', array('store_code' => 'code', 'store_name' => 'name'));

        return $this;
    }


}

?>
